package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeAddQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.xml.crypto.Data;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Api(tags = "01.资讯管理模块")
@Slf4j
@RestController
@RequestMapping("/v1/notice/")
public class NoticeController {


   @Autowired
    NoticeMapper noticeMapper;

    @PostMapping("add")
    @ApiOperation(value = "新增资讯")
    @ApiOperationSupport(order = 10)

    public JsonResult add(NoticeAddParam noticeAddParam) {
        log.debug("noticeAddParam="+noticeAddParam);

        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);
        log.debug("notice = " + notice);
        notice.setUserId(100L);
        notice.setCreateTime(LocalDateTime.now());
        notice.setUpdateTime(LocalDateTime.now());
        log.debug("notice2 = " + notice);
        noticeMapper.insert(notice);
        return JsonResult.ok();
    }

    @GetMapping("list")
    @ApiOperation(value = "资讯列表")
    @ApiOperationSupport(order = 20)


    public JsonResult list(NoticeAddQuery noticeAddQuery){
        log.debug("noticeAddQuery="+noticeAddQuery);
        String title = noticeAddQuery.getTitle();
        Integer type = noticeAddQuery.getType();
        List<NoticeListVO> noticeListVOS =noticeMapper.list(title,type);

        return JsonResult.ok(noticeListVOS);
    }

    @GetMapping("detail")
    @ApiOperation(value = "资讯详情")
    @ApiOperationSupport(order = 30)
    @ApiImplicitParams(value={
            @ApiImplicitParam(name = "id",value = "资讯编号",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "title",value = "资讯标题",required = true,dataType = "String")
    })



    public JsonResult detail(Long id,String title){
        log.debug("id = " + id);

        NoticeDetailInfoVO detailInfoVO = noticeMapper.detail(id);

        return JsonResult.ok(detailInfoVO);
    }
    @PostMapping("del")
    @ApiOperation(value = "删除资讯")
    @ApiOperationSupport(order = 40)
    public JsonResult del(Long id){
        log.debug("id = " + id);
        int i = noticeMapper.del(id);
        if (i > 0){
            return JsonResult.ok();
        }else {
            return new JsonResult(StatusCode.OPERATION_FAIL);
        }
    }

    /**
     * 5.更新资讯功能
     */
    @PostMapping("update")
    @ApiOperation(value = "更新资讯")
    @ApiOperationSupport(order = 50)
    public JsonResult update(@ApiIgnore NoticeUpdateParam noticeUpdateParam){
        log.debug("noticeUpdateParam = " + noticeUpdateParam);
        //更新数据使用实体类
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        //设置更新时间
        notice.setUpdateTime(LocalDateTime.now());
        //调用接口方法
        int i = noticeMapper.update(notice);
        if (i > 0){
            return JsonResult.ok();
        }else {
            return new JsonResult(StatusCode.OPERATION_FAIL);
        }
    }

}
